package sf.actions;

import java.awt.Component;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.swing.JButton;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JPopupMenu.Separator;

import sf.jsonloader.JsonLoader;
import sf.view.StandardForm;
import sf.view.StandardFormFactory;

public class ActionNext extends SFAbstractAction {

	private static final long serialVersionUID = 1L;

	private StandardForm parentForm;

	private JPopupMenu Pmenu;

	private JMenuItem menuItem;

	private String[][] children;

	public ActionNext(StandardForm parentForm) {
		this.parentForm = parentForm;
		putValue(SMALL_ICON, loadIcon("img/next.png"));
		putValue(NAME, "Next");
		putValue(SHORT_DESCRIPTION, "Next");
	}

	public void actionPerformed(ActionEvent e) {
		String table = parentForm.getBrowser().getTableName().toUpperCase();
		AddMenuItems(children, table);
		for (Component c : parentForm.getToolbar().getComponents())
			if (c instanceof JButton) {
				final JButton button = (JButton) c;
				if (button.getAction().toString().contains(
						"sf.actions.ActionNext")) {
					button.addMouseListener(new MouseAdapter() {
						public void mouseClicked(MouseEvent Me) {
							if (button.isEnabled()
									&& Me.getButton() == MouseEvent.BUTTON1)
								Pmenu.show(button, button
										.getVerticalAlignment(), button
										.getHorizontalAlignment()
										+ button.getHeight());
						}
					});
				}
			}
	}

	private int parentChild(String table) {
		JsonLoader loader = JsonLoader.getInstance();
		String[][] s = null;
		try {
			loader.load("exported.json");
			int size = ((ArrayList) loader.get(table, "inRefs")).size();
			s = new String[size][3];
			String code = null;
			for (int i = 0; i < ((ArrayList) loader.get(table, "columns"))
					.size(); i++)
				if (((HashMap) ((ArrayList) loader.get(table, "columns"))
						.get(i)).get("primary").toString().equalsIgnoreCase(
						"true")
						&& ((HashMap) ((ArrayList) loader.get(table, "columns"))
								.get(i)).get("foreign").toString().equals(
								"false")) {
					code = (String) ((HashMap) ((ArrayList) loader.get(table,
							"columns")).get(i)).get("code");
				}
			for (int i = 0; i < size; i++) {
				s[i][0] = (String) ((HashMap) ((ArrayList) loader.get(table,
						"inRefs")).get(i)).get("childTable");
				s[i][1] = (String) loader.get(s[i][0], "name");
				s[i][2] = "false";
				for (int j = 0; j < ((ArrayList) loader.get(s[i][0], "columns"))
						.size(); j++) {
					if (((HashMap) ((ArrayList) loader.get(s[i][0], "columns"))
							.get(j)).get("primary").toString().equals("true")
							&& ((HashMap) ((ArrayList) loader.get(s[i][0],
									"columns")).get(j)).get("code").toString()
									.equals(code))
						s[i][2] = "true";
				}
			}
			String[] tables = new String[size];
			String[] names = new String[size];
			String[] primary = new String[size];
			int poz = 0;
			boolean original = true;
			for (int i = 0; i < names.length; i++) {
				for (int j = i + 1; j < names.length; j++)
					if (s[i][1].equalsIgnoreCase(s[j][1])) {
						original = false;
					}
				if (original) {
					tables[poz] = s[i][0];
					names[poz] = s[i][1];
					primary[poz++] = s[i][2];
				}
				original = true;
			}
			size = poz;
			children = new String[size][3];
			for (int i = 0; i < size - 1; i++)
				for (int j = i; j < size; j++) {
					if (primary[i].compareTo(primary[j]) < 0) {
						String tempt = tables[i];
						String tempn = names[i];
						String tempp = primary[i];
						tables[i] = tables[j];
						names[i] = names[j];
						primary[i] = primary[j];
						tables[j] = tempt;
						names[j] = tempn;
						primary[j] = tempp;
					}
				}
			for (int i = 0; i < size; i++) {
				children[i][0] = tables[i];
				children[i][1] = names[i];
				children[i][2] = primary[i];
			}
		} catch (FileNotFoundException e1) {
			System.out.println("Parent Child problem");
		}
		return s.length;
	}

	private void AddMenuItems(String[][] s, String table) {
		boolean first = true;
		boolean direct = false;
		Pmenu = new JPopupMenu();
		if (s.length == 1)
			StandardFormFactory.createAndShow(s[0][0], null);
		else
			for (int i = 0; i < s.length; i++) {
				if (!s[i][0].equalsIgnoreCase(table)) {
					menuItem = new JMenuItem(s[i][0]);
					menuItem.setText(s[i][1]);
					menuItem.setActionCommand(s[i][0]);
					if (s[i][2].equals("true")) {
						menuItem.setFont(new Font("times new roman", Font.BOLD,
								12));
						direct = true;
					} else if (first) {
						if (direct)
							Pmenu.add(new Separator());
						first = false;	
					}
					menuItem.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent e) {
							StandardFormFactory.createAndShow(e
									.getActionCommand(), null);

						}
					});
					Pmenu.add(menuItem);
				}
			}
	}

	public void setButtonMode(int mode) {
		if (((mode & StandardForm.MODE_EDIT) != 0)
				&& (parentChild(parentForm.getBrowser().getTableName()
						.toUpperCase()) > 0))
			this.setEnabled(true);
		else
			this.setEnabled(false);
	}
}
